package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.basedoc.model.BusinessMessage;
import com.eling.elcms.basedoc.model.TargetType;
import com.eling.elcms.basedoc.model.TaskType;
import com.eling.elcms.basedoc.service.IBusinessMessageManager;
import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.Plan;
import com.eling.elcms.travel.model.Plan.Status;
import com.eling.elcms.travel.model.PlanItem;
import com.eling.elcms.travel.model.view.PlanItemView;
import com.eling.elcms.travel.model.view.PlanView;
import com.eling.elcms.travel.service.IPlanManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class PlanController extends BaseFormController{

	@Autowired
	private IPlanManager planManager;
	@Autowired
	private IBusinessMessageManager businessMessageManager;
	@RequestMapping("/api/plan/save")
    @ResponseBody
	public Plan save(@ModelAttribute@ModelFilling Plan data){
		return planManager.save(data);
	}//增加
	
	@RequestMapping("/api/plan/{pkPlan}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkPlan){
		planManager.remove(pkPlan);
		return new Msg("删除成功");
	}//删除
	
	@RequestMapping("/api/plan/query")
    @ResponseBody
	public List<Plan> query(@ModelAttribute Plan plan){
		return planManager.query(plan);
	}//取列表数据
	
	@RequestMapping("/api/plan/queryyear")
    @ResponseBody
	public List<Plan> queryYear(@ModelAttribute Plan plan){
		return planManager.queryYear(plan);
	}
	@RequestMapping("/api/plan/search*")
	@ResponseBody
	public List<Plan> search(@ModelAttribute SearchCondition sc,@ModelAttribute Plan plan){
		sc.setQueryCondition(plan);
		return planManager.search(sc);
	}//查询
	
	@RequestMapping("/api/plan/saveplan")
    @ResponseBody
	public Plan saveplan(@ModelAttribute PlanView view){
		return planManager.saveplan(view);
	}//view版增加

	@RequestMapping("/api/plan/release")
    @ResponseBody
	public Plan release(@RequestParam(value = "pkPlan") Long pkPlan){
		
		Plan plan = planManager.get(pkPlan);
		
		Plan cond = new Plan();
		cond.setYear(plan.getYear());
		PropertyUtils.setProperty(cond, "yearEnd", plan.getYear());
		List<Plan> plans = planManager.query(cond);
		for(Plan p : plans){
			p.setStatus(Status.Disable);
		}
		planManager.save(plans);
		
		plan.setStatus(Status.Release);
		plan.setPublisher(AppContext.curUser().getName());
		plan.setPublisherDate(new Date());
		plan = planManager.save(plan);
		businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.Mail,plan.getPkPlan()));
		businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.SignUpRegister,plan.getPkPlan()));
		return plan;
	}//发布
	
	@RequestMapping("/api/plan/selectPlanItem")
    @ResponseBody
	public List<PlanItem> selectPlanItem(@ModelAttribute PlanView view){
		return planManager.inquirySignup(view);
	}//查询报名详情
	
	@RequestMapping("/api/plan/selectplan")
    @ResponseBody
	public PlanItemView selectplan(@ModelAttribute Plan plan){
		return planManager.selectplan(plan);
	}
}
